
#import "XLog.h"

@implementation XLog


/**
 * 写日志信息
 * @param        Log信息，写入文件
 * @result
 */
+ (void)writeLog:(NSString *)log {
    NSArray *arrPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *strLogFile = [NSString stringWithFormat:@"%@/log.txt", [arrPath objectAtIndex:0]];
    [log writeToFile:strLogFile atomically:YES encoding:NSUTF8StringEncoding error:nil];
}


/**
 * 记录日志信息
 * @param        log，... 日志信息格式化文本，格式参数
 * @result
 */
+ (void)saveLog:(NSString *)log, ... {
    va_list argList;
    if (log) {
        va_start(argList, log);
        // 格式化输入的日志信息
        NSString *strMsg = [[NSString alloc] initWithFormat:log arguments:argList];
        
        // 获取当前系统日期，并格式化成字符串
        NSDate *date = [NSDate date];
        NSDateFormatter *dateFmt = [[NSDateFormatter alloc] init];
        [dateFmt setDateFormat:@"yyyy-MM-dd HH:mm:ss.SSS"];
        NSString *strDate = [dateFmt stringFromDate:date];
        
        // 格式化输出的日志信息
        NSMutableString *strLog = [[NSMutableString alloc] initWithCapacity:0];
        [strLog appendString:strDate];
        [strLog appendString:@"    "];
        [strLog appendString:strMsg];
        [strLog appendString:@"\r\n"];
        
        // 写入日志文件
        [XLog writeLog:strLog];
        va_end(argList);
    }
}

@end
